{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MeanHamilMinimizer, native with Autograd\n",
    "* Feedback loop between Qubiter and Qubiter\n",
    "* minimization via autograd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## First Example (taken from Pennylane docs). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/rrtucci/PycharmProjects/qubiter/qubiter/jupyter_notebooks\n",
      "/home/rrtucci/PycharmProjects/qubiter\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import sys\n",
    "print(os.getcwd())\n",
    "os.chdir('../../')\n",
    "print(os.getcwd())\n",
    "sys.path.insert(0,os.getcwd())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "np installed? False\n",
      "numpy installed? True\n",
      "autograd.numpy installed? True\n",
      "loaded OneQubitGate, WITH autograd.numpy\n",
      "pu2 in dir True\n",
      "pu2 in sys.modules False\n"
     ]
    }
   ],
   "source": [
    "import qubiter.adv_applications.setup_autograd  # do this first\n",
    "from qubiter.adv_applications.MeanHamil_native import *\n",
    "from qubiter.adv_applications.MeanHamilMinimizer import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_qbits = 2\n",
    "file_prefix = 'mean_hamil_rigetti_test1'\n",
    "emb = CktEmbedder(num_qbits, num_qbits)\n",
    "wr = SEO_writer(file_prefix, emb)\n",
    "wr.write_Rx(0, rads='#1')\n",
    "wr.write_Ry(0, rads='-#2*.5')\n",
    "wr.close_files()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table style='font-family:monospace'><tr><td style='border-right:1px solid red;'>1</td><td style='text-align:left;'><pre>ROTX\t#1\tAT\t0</pre></td></tr><td style='border-right:1px solid red;'>2</td><td style='text-align:left;'><pre>ROTY\t-#2*.5\tAT\t0</pre></td></tr></table>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "wr.print_eng_file(jup=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table style='font-family:monospace'><tr><td style='border-right:1px solid red;'>1</td><td style='text-align:left;'><pre>|   Rx</pre></td></tr><td style='border-right:1px solid red;'>2</td><td style='text-align:left;'><pre>|   Ry</pre></td></tr></table>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "wr.print_pic_file(jup=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "fun_name_to_fun = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hamil=\n",
      " 1.0 [Z0]\n"
     ]
    }
   ],
   "source": [
    "hamil = QubitOperator('Z0', 1.)\n",
    "print('hamil=\\n', hamil)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "init_var_num_to_rads = {1: .3, 2: .8}\n",
    "all_var_nums = [1, 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_samples = 0\n",
    "print_hiatus = 4\n",
    "verbose = False\n",
    "np.random.seed(1234)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "emp_mhamil = MeanHamil_native(file_prefix, num_qbits, hamil,\n",
    "            all_var_nums, fun_name_to_fun, simulator_name='SEO_simulator', num_samples=num_samples)\n",
    "targ_mhamil = MeanHamil_native(file_prefix, num_qbits, hamil,\n",
    "            all_var_nums, fun_name_to_fun, simulator_name='SEO_simulator') # zero samples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "mini = MeanHamilMinimizer(emp_mhamil, targ_mhamil,\n",
    "                 all_var_nums, init_var_num_to_rads,\n",
    "                 print_hiatus=print_hiatus, verbose=verbose)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x_val~ (#1, #2)\n",
      "iter=0, cost=0.575017, targ_cost=0.575017, x_val=0.300000, 0.800000\n",
      "iter=4, cost=0.123982, targ_cost=0.123982, x_val=0.678570, 0.946258\n",
      "iter=8, cost=-0.434184, targ_cost=-0.434184, x_val=1.138412, 0.837671\n",
      "iter=12, cost=-0.804328, targ_cost=-0.804328, x_val=1.451798, 0.595875\n",
      "iter=16, cost=-0.920328, targ_cost=-0.920328, x_val=1.549012, 0.399637\n",
      "iter=20, cost=-0.965189, targ_cost=-0.965189, x_val=1.567496, 0.264552\n",
      "iter=24, cost=-0.984857, targ_cost=-0.984857, x_val=1.570338, 0.174248\n",
      "iter=28, cost=-0.993450, targ_cost=-0.993450, x_val=1.570735, 0.114517\n",
      "iter=32, cost=-0.997175, targ_cost=-0.997175, x_val=1.570788, 0.075189\n",
      "iter=36, cost=-0.998783, targ_cost=-0.998783, x_val=1.570795, 0.049347\n"
     ]
    }
   ],
   "source": [
    "mini.find_min(minlib='autograd', num_iter=40, descent_rate=.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Second, more complicated example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_qbits = 4\n",
    "file_prefix = 'mean_hamil_rigetti_test2'\n",
    "emb = CktEmbedder(num_qbits, num_qbits)\n",
    "wr = SEO_writer(file_prefix, emb)\n",
    "wr.write_Ry(2, rads=np.pi/7)\n",
    "wr.write_Ry(1, rads='#2')\n",
    "wr.write_Rx(1, rads='#1')\n",
    "wr.write_cnot(2, 3)\n",
    "wr.write_qbit_swap(1, 2)\n",
    "wr.close_files()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table style='font-family:monospace'><tr><td style='border-right:1px solid red;'>1</td><td style='text-align:left;'><pre>ROTY\t25.714286\tAT\t2</pre></td></tr><td style='border-right:1px solid red;'>2</td><td style='text-align:left;'><pre>ROTY\t#2\tAT\t1</pre></td></tr><td style='border-right:1px solid red;'>3</td><td style='text-align:left;'><pre>ROTX\t#1\tAT\t1</pre></td></tr><td style='border-right:1px solid red;'>4</td><td style='text-align:left;'><pre>SIGX\tAT\t3\tIF\t2T</pre></td></tr><td style='border-right:1px solid red;'>5</td><td style='text-align:left;'><pre>SWAP\t2\t1</pre></td></tr></table>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "wr.print_eng_file(jup=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table style='font-family:monospace'><tr><td style='border-right:1px solid red;'>1</td><td style='text-align:left;'><pre>|   Ry  |   |</pre></td></tr><td style='border-right:1px solid red;'>2</td><td style='text-align:left;'><pre>|   |   Ry  |</pre></td></tr><td style='border-right:1px solid red;'>3</td><td style='text-align:left;'><pre>|   |   Rx  |</pre></td></tr><td style='border-right:1px solid red;'>4</td><td style='text-align:left;'><pre>X---@   |   |</pre></td></tr><td style='border-right:1px solid red;'>5</td><td style='text-align:left;'><pre>|   <--->   |</pre></td></tr></table>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "wr.print_pic_file(jup=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "fun_name_to_fun = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hamil=\n",
      " 0.7 [X1 Y2] +\n",
      "0.4 [Y1 X2 Y3]\n"
     ]
    }
   ],
   "source": [
    "hamil = QubitOperator('X1 Y3 X1 Y1 X2', .4) + QubitOperator('Y2 X1', .7)\n",
    "print('hamil=\\n', hamil)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "init_var_num_to_rads = {1: 2.1, 2:1.2}\n",
    "all_var_nums = [1, 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_samples = 0\n",
    "print_hiatus = 2\n",
    "verbose = False\n",
    "np.random.seed(1234)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "emp_mhamil = MeanHamil_native(file_prefix, num_qbits, hamil,\n",
    "            all_var_nums, fun_name_to_fun, simulator_name='SEO_simulator', num_samples=num_samples)\n",
    "targ_mhamil = MeanHamil_native(file_prefix, num_qbits, hamil,\n",
    "            all_var_nums, fun_name_to_fun, simulator_name='SEO_simulator') # zero samples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "mini = MeanHamilMinimizer(emp_mhamil, targ_mhamil,\n",
    "                 all_var_nums, init_var_num_to_rads,\n",
    "                 print_hiatus=print_hiatus, verbose=verbose)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x_val~ (#1, #2)\n",
      "iter=0, cost=-0.211239, targ_cost=-0.211239, x_val=2.100000, 1.200000\n",
      "iter=2, cost=-0.248413, targ_cost=-0.248413, x_val=2.100000, 1.111845\n",
      "iter=4, cost=-0.273138, targ_cost=-0.273138, x_val=2.100000, 1.039734\n",
      "iter=6, cost=-0.288820, targ_cost=-0.288820, x_val=2.100000, 0.982194\n",
      "iter=8, cost=-0.298464, targ_cost=-0.298464, x_val=2.100000, 0.937017\n",
      "iter=10, cost=-0.304281, targ_cost=-0.304281, x_val=2.100000, 0.901905\n",
      "iter=12, cost=-0.307749, targ_cost=-0.307749, x_val=2.100000, 0.874784\n",
      "iter=14, cost=-0.309801, targ_cost=-0.309801, x_val=2.100000, 0.853911\n",
      "iter=16, cost=-0.311011, targ_cost=-0.311011, x_val=2.100000, 0.837884\n",
      "iter=18, cost=-0.311723, targ_cost=-0.311723, x_val=2.100000, 0.825593\n"
     ]
    }
   ],
   "source": [
    "mini.find_min(minlib='autograd', num_iter=20, descent_rate=.1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "toc": {
   "colors": {
    "hover_highlight": "#DAA520",
    "running_highlight": "#FF0000",
    "selected_highlight": "#FFD700"
   },
   "moveMenuLeft": true,
   "nav_menu": {
    "height": "12px",
    "width": "252px"
   },
   "navigate_menu": true,
   "number_sections": true,
   "sideBar": true,
   "threshold": 4,
   "toc_cell": false,
   "toc_section_display": "block",
   "toc_window_display": false,
   "widenNotebook": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
